package com.service;

import com.mapper.entity.StockCirculationDetail;
import com.mapper.entity.mapper.StockCirculationDetailMapper;
import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.service
 * @date:2021/9/16
 */

@Component
public class StockDetailInsertHelper {

    @Autowired
    StockCirculationDetailMapper stockCirculationDetailMapper;

    public void insertDetail(long type,String code, StockCirculationDetail detail){
        HintManager hintManager = HintManager.getInstance();
        String last = code.substring(code.length()-1);
        String second = code.substring(code.length()-2,code.length()-1);
        hintManager.addDatabaseShardingValue("_stock_circulation_detail",last);
        hintManager.addTableShardingValue("_stock_circulation_detail","first_"+last);
        hintManager.addTableShardingValue("_stock_circulation_detail","second_"+second);
        hintManager.addTableShardingValue("_stock_circulation_detail",String.valueOf(type));
        stockCirculationDetailMapper.insertSelectiveReturnId(detail);
        hintManager.close();
    }

    public void updateDetail(long type,String code, StockCirculationDetail detail){
        HintManager hintManager = HintManager.getInstance();
        String last = code.substring(code.length()-1);
        String second = code.substring(code.length()-2,code.length()-1);
        hintManager.addDatabaseShardingValue("_stock_circulation_detail",last);
        hintManager.addTableShardingValue("_stock_circulation_detail","first_"+last);
        hintManager.addTableShardingValue("_stock_circulation_detail","second_"+second);
        hintManager.addTableShardingValue("_stock_circulation_detail",String.valueOf(type));
        stockCirculationDetailMapper.updateByPrimaryKeySelective(detail);
        hintManager.close();
    }



    public void delete(long type,String code, long detail) {
        HintManager hintManager = HintManager.getInstance();
        String last = code.substring(code.length() - 1);
        String second = code.substring(code.length() - 2, code.length() - 1);
        hintManager.addDatabaseShardingValue("_stock_circulation_detail", last);
        hintManager.addTableShardingValue("_stock_circulation_detail", "first_" + last);
        hintManager.addTableShardingValue("_stock_circulation_detail", "second_" + second);
        hintManager.addTableShardingValue("_stock_circulation_detail", String.valueOf(type));
        stockCirculationDetailMapper.deleteByPrimaryKey(detail);
        hintManager.close();
    }
}
